package com.example.docking.service.impl;

import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.request.SalOrderRequest;
import com.example.docking.dao.jindie.response.MaterialEntity;
import com.example.docking.dao.jindie.response.SaveData;
import com.example.docking.service.SalesOrderService;
import com.example.docking.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class SalesOrderServiceImpl extends BaseServiceImpl implements SalesOrderService {
    @Override
    public ApiResult SelectSalesOrderList() throws Exception {
        return sendApiRequestGet(Constants.SAL_ORDER, map, "");
    }

    @Override
    public ApiResult InsertSalesOrder(SalOrderRequest salOrderRequest) throws Exception {
        return sendApiRequestPostBody(Constants.SAL_ORDER, map, salOrderRequest);
    }

    @Override
    public ApiResult SelectSalesOrderDetail() throws Exception {
        map.put("id", "2184799947029834752");
        return sendApiRequestGet(Constants.SAL_ORDER_DETAIL, map, "");
    }

    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();
        SalesOrderService service = new SalesOrderServiceImpl();

        ApiResult result = service.SelectSalesOrderList();
//        ResponseData<GenericDataItem<SalesOrderData>> response = gson.fromJson(result.getBody(), ResponseData.class);
//        System.out.println("Code: " + response.getErrcode());
//        System.out.println("Message: " + response.getDescription());
//        System.out.println("data: " + response.getData());
        SalOrderRequest salOrderRequest = new SalOrderRequest();
        salOrderRequest.setBill_date("2025-04-07");
        salOrderRequest.setCustomer_id("1");
        //商品分录
        List<MaterialEntity> materialEntityList = new ArrayList<>();
        MaterialEntity materialEntity = new MaterialEntity();
        materialEntity.set_free(false);
        materialEntity.setMaterial_id("2184076435763759104");
//        materialEntity.setPrice("50000");
        materialEntity.setQty(22);
        materialEntity.setUnit_id("2177772470629580800");
        materialEntityList.add(materialEntity);
        salOrderRequest.setMaterial_entity(materialEntityList);
        Type type1 = new TypeToken<ResponseData<SaveData>>() {
        }.getType();
        ApiResult result1 = service.InsertSalesOrder(salOrderRequest);
        ResponseData<SaveData> response = gson.fromJson(result1.getBody(), type1);
        System.out.println("Code: " + response.getErrcode());
        System.out.println("Message: " + response.getDescription());
        System.out.println("data: " + response.getData());
    }

//    public static void main(String[] args) throws Exception {
//        Gson gson = new Gson();
//        SalesOrderService service = new SalesOrderServiceImpl();
//        ApiResult result = service.SelectSalesOrderDetail();
//        ResponseData<SalOrderDetailData> response = gson.fromJson(result.getBody(), ResponseData.class);
//        System.out.println("Code: " + response.getErrcode());
//        System.out.println("Message: " + response.getDescription());
//        System.out.println("data: " + response.getData());
//
//    }
}
